package org.swust.sysmonitorapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.swust.sysmonitorapp.dto.SysUserDto;
import org.swust.sysmonitorapp.entity.CountDepartment;
import org.swust.sysmonitorapp.entity.QSysUser;
import org.swust.sysmonitorapp.entity.SysUser;
import org.swust.sysmonitorapp.mapper.SysDepartmentMapper;
import org.swust.sysmonitorapp.mapper.SysUserMapper;
import org.swust.sysmonitorapp.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 用户表 服务实现类
 *
 * @since 2024-12-24
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private SysDepartmentMapper deptMapper;

    @Override
    public IPage<SysUserDto> pageUser(Page<SysUserDto> page, QSysUser qSysUser) {
        return baseMapper.pageUser(page, qSysUser);
    }

    @Override
    public boolean editPassword(Integer id, String password) {
        return baseMapper.updatePassword(id, password) > 0;
    }


    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    @Override
    public boolean resetPwd(Integer id) {
        String pwd = ENCODER.encode("123456");
        return baseMapper.resetPwd(id , pwd) > 0 ? true : false;
    }

    @Override
    public List<CountDepartment> countDepartment() {
        return baseMapper.countDepartment();
    }
    
    @Override
    public SysUser getUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName, username);
        return getOne(queryWrapper);
    }

    @Override
    public boolean removeById(Serializable id) {
        Long deptId = baseMapper.selectById(id).getDeptId();
        boolean result = super.removeById(id);
        if (result) {
            deptMapper.changeCount(deptId, -1);
        }
        return result;
    }

    @Override
    public boolean save(SysUser entity) {
        // 获取并加密密码
        String pwd = ENCODER.encode(entity.getPassword());
        // 将加密后的密码放回实体
        entity.setPassword(pwd);
        boolean result = super.save(entity);
        if (result) {
            deptMapper.changeCount(entity.getDeptId(), 1);
        }
        return result;
    }

    @Override
    public boolean updateById(SysUser entity) {
        // 获取并加密密码
        String pwd = ENCODER.encode(entity.getPassword());
        // 将加密后的密码放回实体
        entity.setPassword(pwd);
        boolean result = super.updateById(entity);
        if (result) {
            deptMapper.changeCount(entity.getDeptId(), 1);
        }
        return result;
    }
}
